<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="未发货合同商品"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <div>
      <notYetShipped ref="notYetShipped"></notYetShipped>
    </div>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="onSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
  <script>
import VolBox from "@/components/basic/VolBox.vue";
import notYetShipped from "@/views/salesinfo/not_yet_shipped/q_not_yet_shipped.vue";
import { mapMutations } from "vuex";
import { onUnmounted } from "vue";
//这里使用的vue2语法，也可以写成vue3语法
export default {
  setup() {
    onUnmounted(() => {
      console.log("2页面销毁");
    });
  },
  components: {
    "vol-box": VolBox,
    notYetShipped,
  },
  data() {
    return {
      model: false,
    };
  },
  beforeDestroy() {
    console.log("页面销毁");
  },
  methods: {
    ...mapMutations(("notYetShipped", ["storeChangeForm", "storeRemoveForm"])),
    async onModelClose() {
      this.$refs.notYetShipped.searchFormFields.Voucher_sub = null;
      this.$refs.notYetShipped.searchFormFields.Goods_sub = null;
      this.$refs.notYetShipped.searchFormFields.Goods_name = null;
      this.$refs.notYetShipped.searchFormFields.weifahuo = ["", ""];
    },
    open() {
      this.model = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.notYetShipped) {
          clearInterval(setIntervalTiem);
          this.$refs.notYetShipped.$refs.grid.search();
        }
      }, 0);
    },

    async onSelect() {
      let rowObj = this.$refs.notYetShipped.$refs.grid.getSelectRows();
      if (!rowObj.length) {
        this.base.ElMessageFn("请至少选择一条数据");
        return;
      }
      this.$emit("parentCall", ($parent) => {
        try {
          let arr = [];
          //处理明细表已含有相同数据
          let parentList = $parent.$refs.detail.rowData;
          let set = new Set(parentList.map((item) => item.Goods_sub));//得到由明细表goods_sub组成的数组
          let commonObjects = rowObj.filter((item) => set.has(item.Goods_sub)); //得到明细表中已包含的选中行
          let goodsNamesSet = new Set( commonObjects.map((item) => item.Goods_sub)); //相同商品的编码
          if (commonObjects.length) {
            rowObj = rowObj.filter((item) => !goodsNamesSet.has(item.Goods_sub)); //在选中数据中剔除明细表已包含的行数据
            let str = "";
            commonObjects.forEach((r) => {
              if (str == "") str += r.Goods_name;
              else str = str + "," + r.Goods_name;
            });
            this.$message.warning(`商品${str}已存在此发货单`);
          }
          arr = rowObj.map((item) => {
            return {
              Goods_sub: item.Goods_sub,
              Goods_name: item.Goods_name,
              Contract_goods_num: item.weifahuo,
              Goods_num: item.weifahuo,
              Goods_size: item.Goods_size,
              Goods_price: item.Goods_price,
              Total_price: item.Total_price,
              Logistics_status: item.Logistics_status,
              Install_status: item.Install_status,
              sales_contractList_ID: item.Sales_contractList,
              Sales_contract_voucher_sub: item.Voucher_sub,
              Tax_exclusive_total_price: item.Tax_exclusive_total_price,
            };
          });
          $parent.$refs.detail.rowData.unshift(...arr);
        } catch (error) {}
      });
      this.onModelClose();
      return (this.model = false);
    },
  },
};
</script>